package ltd.smallwang;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author smallwang
 * @email justdoalitter@gmail.com
 * @create date 2025-01-10 22:17:54
 * @modify date 2025-01-10 22:17:54
 * @desc [description]
 */
public class Leetcode0205 {

  public boolean isIsomorphic(String s, String t) {
    Map<Character, Character> s2t = HashMap.newHashMap(10);
    Map<Character, Character> t2s = HashMap.newHashMap(10);
    int len = s.length();
    for (int i = 0; i < len; i++) {
      Character sItem = s.charAt(i);
      Character tItem = t.charAt(i);
      if (s2t.containsKey(sItem)) {
        if (tItem.equals(s2t.get(sItem))) {
          return false;
        }
      } else {
        if (t2s.containsKey(tItem)) {
          return false;
        }
      }
      if (t2s.containsKey(tItem)) {
        if (sItem.equals(t2s.get(tItem))) {
          return false;
        }
      } else {
        if (s2t.containsKey(sItem)) {
          return false;

        }
      }

      s2t.put(sItem, tItem);
      t2s.put(tItem, sItem);
    }
    return true;
  }

  @SuppressWarnings("unused")
  private boolean method01(String s, String t) {
    int len = s.length();
    int[] recordArr = new int[128];
    int[] countArr = new int[128];
    Arrays.fill(recordArr, Integer.MAX_VALUE);
    for (int i = 0; i < len; i++) {
      int index = s.charAt(i) - ' ';
      int gap = t.charAt(i) - s.charAt(i);
      countArr[t.charAt(i) - ' '] += 1;
      if (recordArr[index] == Integer.MAX_VALUE) {
        recordArr[index] = gap;
        if (countArr[t.charAt(i) - ' '] > 1) {
          return false;
        }
      } else {
        if (recordArr[index] != gap) {
          return false;
        }
      }
    }
    return true;
  }
}
